package com.butnet.myframe.creator;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

public class Libs {
	private static List<String> libs = null;

	public static List<String> getLibs() {
		if (libs == null) {
			libs = new LinkedList<String>();
			InputStream in = null;
			BufferedReader r = null;
			try {
				in = Libs.class.getResourceAsStream("libs.conf");
				r = new BufferedReader(new InputStreamReader(in));
				String line = null;
				while ((line = r.readLine()) != null)
					libs.add(line.trim());
			} catch (Exception e) {
				if (r != null) {
					try {
						r.close();
					} catch (Exception ex) {
					}
				}
				if (in != null) {
					try {
						in.close();
					} catch (Exception ex) {
					}
				}
				e.printStackTrace();
			}
		}
		return libs;
	}

	/**
	 * 复制库
	 * 
	 * @param webRoot
	 * @throws IOException
	 */
	public static void copyLibs(File webRoot) throws IOException {
		getLibs();
		for (String r : libs) {
			copyResouse("/com/butnet/myframe/res/lib/" + r, new File(webRoot, "WEB-INF/lib/" + r));
		}
	}

	private static void copyResouse(String res, File f) throws IOException {
		InputStream in = Libs.class.getResourceAsStream(res);
		FileOutputStream out = new FileOutputStream(f);
		byte[] buff = new byte[1024 * 10];
		int num = -1;
		while ((num = in.read(buff)) != -1) {
			out.write(buff, 0, num);
		}
		out.close();
		in.close();
	}
}
